/*
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
*/

#include <iostream>
using namespace std; 
#include "cmdOption.h" 
#include "cmdArg.h" 
#include "ipParser.h"
#include "cmdLine.h" 

const char* printVerbOpt(const char* str)
{
	if (str == NULL)
		return "not set";
	else
		return str;
}

int main(int argc, char **argv) {
	

	cmdLine cmdParser("cmdLine class test program");
	// options
	cmdParser.AddOption('i', "ignore", "ignore errors");
	cmdParser.AddOption('b', "build", "build something");
	cmdParser.AddOption('B', "block", "block something");
    // string options
	cmdParser.AddStrOption('s', "string", "some optional string");

	// option arg
	cmdParser.AddOptionArg<int>('d', "debug-level", "debug level", "%d");
	cmdParser.AddOptionArg<long>('l', "long", "%ld");
	cmdParser.AddOptionArg<unsigned int>('x', "hex", "%X");
	cmdParser.AddOptionArg<Ip_t,ipParser>('h', "host", "ip address x.x.x.x");
	// args
	cmdParser.AddStrArg("some bloody argument..", false);
	cmdParser.AddArg<int>("%d", "some int argument");


	if (cmdParser.Parse(argc, argv) != ERR_NONE)
		exit(0);

	cout << endl << "Options set: (" << cmdParser.OptCount() << " options recieved)" << endl;

	// get list of all keys in command line
	string s = cmdParser.OptionKeyList();
	
	// print all options 
	for(int n = 0; n < (int)s.size(); n++) {
		AbstractOption& op = cmdParser[s[n]];
		cout << op.Key() << "\t" << printVerbOpt(op.VerboseKey());
		
		if (op.ExpectValue()) {
			
			if (op.Key() == 'h') {
				
				Ip_t ip = cmdParser.Value<Ip_t>('h');
				cout  << "=" << ip.ip[0] << "." << ip.ip[1] << "." << ip.ip[2] << "." << ip.ip[3];
			}
			else // it'll not allways work 
				cout  << "=" << cmdParser.Value<long>(s[n]);
		}
		cout << endl;
	}
	
	cout << endl << "Arguments: (" << cmdParser.ArgCount() << " recieved)" << endl;
	
	if (cmdParser.ArgCount() > 0) {
		cmdParser[0].PrintDecs(cout);
		const char* str =  cmdParser.Value<const char*>(0);
		//char ptrval[22];
		//sprintf(ptrval, "%p", str);
		//cout << "\t" << ptrval << endl;
		cout << "\t" << str << endl;
	}

	if (cmdParser.ArgCount() > 1) {
		cmdParser[1].PrintDecs(cout);
		cout << "\t" << cmdParser.Value<int>(1)<< endl;;
	}
	// how we are using it 
	if (cmdParser.IsKeySet('h')) {

		Ip_t ip = cmdParser.Value<Ip_t>('h');
		cout  << "=" << ip.ip[0] << "." << ip.ip[1] << "." << ip.ip[2] << "." << ip.ip[3] << endl;
	}

	return 0;
}
